export function formatTime(timestamp, format) {
    if(timestamp === undefined && format === undefined) timestamp = Date.now();
    if(format === undefined && typeof timestamp === 'string' && timestamp !== '' && isNaN(timestamp)) {
        format = timestamp;
        timestamp = Date.now();
    }
    let od = new Date(timestamp);
    if(isNaN(od.getTime())) throw new Error('Invalid Date');
    let y = od.getFullYear().toString(),
        M = od.getMonth() + 1,
        d = od.getDate(),
        H = od.getHours(),
        m = od.getMinutes(),
        s = od.getSeconds();
    let o = {
        yyyy: y,
        yyy: y.substr(1),
        yy: y.substr(2),
        y: y.substr(3),
        MM: M < 10 ? '0' + M : M,
        M: M,
        dd: d < 10 ? '0' + d : d,
        d: d,
        HH: H < 10 ? '0' + H : H,
        H: H,
        mm: m < 10 ? '0' + m : m,
        m: m,
        ss: s < 10 ? '0' + s : s,
        s: s,
    };
    if(!format || typeof format !== 'string') format = 'yyyy-MM-dd HH:mm:ss';
    return format.replace(/y{1,4}|M{1,2}|d{1,2}|H{1,2}|m{1,2}|s{1,2}/g, a => o[a]);
}

export function getPoster(url, time) {
    return new Promise((resolve, reject) => {
        const cv = document.createElement('canvas')
        const ctx = cv.getContext('2d')
        const vd = document.createElement('video')
        vd.src = url
        vd.currentTime = time || 0.001
        vd.onloadeddata = e => {
            cv.width = vd.videoWidth
            cv.height = vd.videoHeight
            ctx.drawImage(vd, 0, 0)
            cv.toBlob(blob => {
                resolve(window.URL.createObjectURL(blob))
            })
        }
    })
}